package com.example.smartmedicalsystem;

import com.example.smartmedicalsystem.entity.Department;
import com.example.smartmedicalsystem.entity.User;
import com.example.smartmedicalsystem.repository.DepartmentRepository;
import com.example.smartmedicalsystem.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class DataInitializer {

    @Bean
    public CommandLineRunner loadAdmin(UserRepository userRepository, PasswordEncoder passwordEncoder) {
        return args -> {
            if (userRepository.findByUsername("admin") == null) {
                User admin = new User();
                admin.setUsername("admin");
                admin.setPassword(passwordEncoder.encode("admin123"));
                admin.setRole(User.Role.ADMIN);
                userRepository.save(admin);
            }
        };
    }

    @Bean
    public CommandLineRunner loadDepartments(DepartmentRepository departmentRepository) {
        return args -> {
            if (departmentRepository.count() == 0) {
                departmentRepository.save(new Department(null, "内科", "处理内科疾病"));
                departmentRepository.save(new Department(null, "外科", "处理外科手术"));
                departmentRepository.save(new Department(null, "儿科", "处理儿童疾病"));
            }
        };
    }
}
